//
//  ThemeButton.m
//  WXWeibo
//
//  Created by apple on 16/5/10.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "ThemeButton.h"

@implementation ThemeButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self _observeThemeChangeNotification];
    }
    return self;
}

-(void)awakeFromNib {
    
    [super awakeFromNib];
    [self _observeThemeChangeNotification];
}

- (void)_observeThemeChangeNotification {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeChangeAction:) name:kThemeChangeNotification object:nil];
    
    
}


- (void)themeChangeAction:(NSNotification *)notification {
  
    [self loadImage];
    
    
}

- (void)setNormalImageName:(NSString *)normalImageName {
    
    if (_normalImageName != normalImageName) {
        _normalImageName = [normalImageName copy];
        [self loadImage];
    }
}

- (void)setHighlightImageName:(NSString *)highlightImageName {
    
    if (_highlightImageName != highlightImageName) {
        _highlightImageName = [highlightImageName copy];
        [self loadImage];
    }
}

- (void)setBackgroundImageName:(NSString *)backgroundImageName {
    
    if (_backgroundImageName != backgroundImageName) {
        _backgroundImageName = [backgroundImageName copy];
        [self loadImage];
    }
    
}

- (void)loadImage {
    
    ThemeManager *manager = [ThemeManager shareManager];
    UIImage *normalImg = [manager themeImageWithImageName:self.normalImageName];
    UIImage *highlightImg = [manager themeImageWithImageName:self.highlightImageName];
    UIImage *backImg = [manager themeImageWithImageName:self.backgroundImageName];
    if (normalImg) {
        
        [self setImage:normalImg forState:UIControlStateNormal];
    }
    
    if (highlightImg) {
        
        [self setImage:highlightImg forState:UIControlStateHighlighted];
    }
    if (backImg) {
        [self setBackgroundImage:backImg forState:UIControlStateNormal];
    }
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kThemeChangeNotification object:nil];
}
@end
